﻿
using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Writer.API.Application.Commends;
using MediatR;
using System.Linq;

namespace B.S.Traceability.Writer.API.Application.CommendHandlers
{
    public class DatchDelCommendHandlers : IRequestHandler<DatchDelCommends, APIResult<Products>>
    {

        private readonly IBaseRepository<Products> productsRepository;

        public DatchDelCommendHandlers(IBaseRepository<Products> productsRepository)
        {
            this.productsRepository = productsRepository;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Products>> Handle(DatchDelCommends request, CancellationToken cancellationToken)
        {
            APIResult<Products> result=new APIResult<Products>();
            result.Code = APIEnum.删除成功;
            result.Msg = "批量删除成功";
            var list = productsRepository.GetAllAsync().Result.Where(p => request.ProductsId.Contains(p.ProductsId)).ToList();
            foreach (var item in list)
            {
                item.IsDel=!item.IsDel;
            }
            await productsRepository.UpDataAsync(list);
            return  await Task.FromResult(result);
        }

      /*  /// <summary>
        /// 批量删除订单
        /// </summary>
        /// <param name="OrderIds"></param>
        /// <returns></returns>
        public async Task<ApiResult> BatchDelOrderAsync(List<long> OrderIds)
        {
            try
            {
                var result = new ApiResult<OrderInfo>("删除失败");
                var list = (await orderRepository.GetAllAsync(p => OrderIds.Contains(p.OrderId))).ToList();

                foreach (var item in list)
                {
                    item.IsDelete = 1;
                }

                var res = await orderRepository.UpDataAsync(list);
                if (res) result.Success();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }*/

    }
}
